package theOrbalEra.common.block;

import theOrbalEra.common.TheOrbalEra;
import theOrbalEra.common.item.OrbalItemQuartz;
import theOrbalEra.common.item.OrbalItemOrbment;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityOrballFactory extends TileEntity implements
		IInventory {

	public ItemStack[] OrbalFacCon = new ItemStack[7];
	public boolean[] line = new boolean[5];
	public boolean[] point = { true, false, false, false, false, false };

	@Override
	public void closeChest() {

	}

	@Override
	public ItemStack decrStackSize(int var1, int var2) {
	
		if (var1 == 0) {
			for (int i = 1; i < 7; i++) {
				OrbalFacCon[i] = null;
			}
		}
		if (OrbalFacCon[var1] != null) {
			ItemStack var3;

			if (OrbalFacCon[var1].stackSize <= var2) {
				var3 = OrbalFacCon[var1].copy();
				OrbalFacCon[var1] = null;
				
			
				return var3;
			} else {
				var3 = OrbalFacCon[var1].splitStack(var2);

				if (OrbalFacCon[var1].stackSize == 0) {
					OrbalFacCon[var1] = null;
				}

				return var3;
			}
		} else {
			return null;
		}
	}

	@Override
	public int getInventoryStackLimit() {
		
		return 1;
	}

	@Override
	public String getInvName() {
		
		return "Glorious";
	}

	public boolean getLine(int par1) {
		return line[par1];
	}

	public boolean getPoint(int par1) {
		return point[par1];
	}

	@Override
	public int getSizeInventory() {
		
		return OrbalFacCon.length;
	}

	

	@Override
	public ItemStack getStackInSlot(int var1) {
		
		return OrbalFacCon[var1];
	}

	@Override
	public ItemStack getStackInSlotOnClosing(int var1) {
		
		if (OrbalFacCon[var1] != null) {
			ItemStack var2 = OrbalFacCon[var1];
			OrbalFacCon[var1] = null;
			return var2;
		} else {
			return null;
		}

	}

	

	@Override
	public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
		return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this ? false
				: par1EntityPlayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D,
						zCoord + 0.5D) <= 64.0D;
	}

	@Override
	public void openChest() {


	}

	@Override
	public void readFromNBT(NBTTagCompound par1nbtTagCompound) {
		super.writeToNBT(par1nbtTagCompound);
		NBTTagList var2 = par1nbtTagCompound.getTagList("Items");
		this.OrbalFacCon = new ItemStack[this.getSizeInventory()];

		for (int var3 = 0; var3 < var2.tagCount(); ++var3) {
			NBTTagCompound var4 = (NBTTagCompound) var2.tagAt(var3);
			byte var5 = var4.getByte("Slot");
			if (var5 >= 0 && var5 < this.OrbalFacCon.length) {
				this.OrbalFacCon[var5] = ItemStack.loadItemStackFromNBT(var4);
			}
		}

		
	}

	@Override
	public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
		if (par1 != 0 && par2ItemStack != null) {
			if (OrbalFacCon[0] != null&& OrbalFacCon[0].getItem() instanceof OrbalItemOrbment){
				int[] cao = OrbalItemOrbment.getCao(OrbalFacCon[0]);
				int[] ele = OrbalItemOrbment.getEle(OrbalFacCon[0]);
				int[] able = OrbalItemOrbment.getAble(OrbalFacCon[0]);
			if (cao[par1 - 1]==1)
				OrbalFacCon[par1] = par2ItemStack.copy();
				if (par2ItemStack.stackSize > getInventoryStackLimit()) {
					par2ItemStack.stackSize = getInventoryStackLimit();
				}
				if(par2ItemStack.getItem() instanceof OrbalItemQuartz){
					ele[par1-1]=OrbalItemQuartz.getEle(par2ItemStack);
					able[par1-1]=OrbalItemQuartz.getEle(par2ItemStack);
					OrbalItemOrbment.setEle(OrbalFacCon[0], ele);
					OrbalItemOrbment.setAble(OrbalFacCon[0], able);
				}
					
				onInventoryChanged();
			} else if (!worldObj.isRemote) {
				worldObj.spawnEntityInWorld(new EntityItem(worldObj, xCoord,yCoord, zCoord, par2ItemStack));
			}
		} 
		else if (par1 == 0 && par2ItemStack != null) {
			OrbalFacCon[par1] = par2ItemStack.copy();
			if (par2ItemStack.getItem() instanceof OrbalItemOrbment) {
				ItemStack Orb =par2ItemStack.copy();
				int[] able=OrbalItemOrbment.getAble(Orb);
				int[] ele=OrbalItemOrbment.getEle(Orb);
				int[] cao=OrbalItemOrbment.getCao(Orb);
				for (int i = 1; i <OrbalFacCon.length ; i++) {
					if (cao[i-1]==1&&able[i-1] != -1) {
						OrbalFacCon[i] = null;
						ItemStack loop=new ItemStack(TheOrbalEra.quartz,1,ele[i-1]);
						OrbalItemQuartz.setAble(loop, able[i-1]);
						OrbalItemQuartz.setEle(loop, ele[i-1]);
						OrbalFacCon[i] = loop;
						
					} else {
						OrbalFacCon[i] = null;
					}
				}
			}
			onInventoryChanged();

		} else {
		}
	}

	public void setLine(int par1, boolean par2) {
		line[par1] = par2;
	}

	public void setPoint(int par1, boolean par2) {
		point[par1] = par2;
	}

	@Override
	public void updateEntity() {
		
		if (OrbalFacCon[0] != null&& OrbalFacCon[0].getItem() instanceof OrbalItemOrbment) {
		    ItemStack orb = OrbalFacCon[0];
		    int[] cao=OrbalItemOrbment.getCao(orb);
		    int[] orbele=OrbalItemOrbment.getEle(orb);
		    int[] orbable=OrbalItemOrbment.getAble(orb);
		    boolean[]point = {true,false,false,false,false,false};
		    for(int i=0;i<cao.length;i++){
		    	if(cao[i]==1)
		    		point[i]=true;
		    	else
		    		point[i]=false;
		    }
		    	
			for (int i = 0; i < 6; i++) {
			this.setPoint(i, point[i]);	
			}
			
			for (int i = 0; i < 6; i++) {
				if (OrbalFacCon[i + 1] != null){
					orbele[i]=OrbalItemQuartz.getEle(OrbalFacCon[i + 1]);
					orbable[i]=OrbalItemQuartz.getAble(OrbalFacCon[i + 1]);
				} 
				else {
					orbele[i]=-1;
					orbable[i]=-1;
				}
			}
			OrbalItemOrbment.setAble(orb, orbable);
			OrbalItemOrbment.setEle(orb, orbele);
			
			if (OrbalFacCon[1] != null && OrbalFacCon[2] != null) {
				setLine(0, true);
			} else {
				setLine(0, false);
			}
			if (OrbalFacCon[2] != null && OrbalFacCon[3] != null) {
				setLine(1, true);
			} else {
				setLine(1, false);
			}
			if (OrbalFacCon[3] != null && OrbalFacCon[4] != null) {
				setLine(2, true);
			} else {
				setLine(2, false);
			}
			if (OrbalFacCon[1] != null && OrbalFacCon[5] != null) {
				setLine(3, true);
			} else {
				setLine(3, false);
			}
			if (OrbalFacCon[5] != null && OrbalFacCon[6] != null) {
				setLine(4, true);
			} else {
				setLine(4, false);
			}
			if (OrbalFacCon[1] != null && OrbalFacCon[2] != null) {
				setLine(0, true);
			} else {
				setLine(0, false);
			}
		}
		else{
			for(int i=0;i<5;i++){
				this.setLine(i, false);
			}
			for(int i=0;i<6;i++){
				this.setPoint(i, false);
			}
		}

	}

	// ItemOrbment orb ;
	@Override
	public void writeToNBT(NBTTagCompound par1nbtTagCompound) {
		
		super.writeToNBT(par1nbtTagCompound);
		
		
		NBTTagList var2 = new NBTTagList();

		for (int var3 = 0; var3 < this.OrbalFacCon.length; ++var3) {
			if (this.OrbalFacCon[var3] != null) {
				NBTTagCompound var4 = new NBTTagCompound();
				var4.setByte("Slot", (byte) var3);
				this.OrbalFacCon[var3].writeToNBT(var4);
				var2.appendTag(var4);
			}
		}

		par1nbtTagCompound.setTag("Items", var2);
	}




	@Override
	public boolean isInvNameLocalized() {
		return false;
	}

	@Override
	public boolean isStackValidForSlot(int i, ItemStack itemstack) {
		return false;
	}

	
	

}
